<template>
  <div class="reset-password-container">
    <div class="form-header">
      <div class="header-icon"></div>
      <div class="header-title">修改密码</div>
      <div class="train-decoration"></div>
    </div>
    
    <el-form ref="form" :model="user" :rules="rules" label-width="85px" class="password-form">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input 
          v-model="user.oldPassword" 
          placeholder="请输入旧密码" 
          type="password" 
          show-password
          prefix-icon="el-icon-lock"
          class="custom-input"
        />
      </el-form-item>
      
      <el-form-item label="新密码" prop="newPassword">
        <el-input 
          v-model="user.newPassword" 
          placeholder="请输入新密码" 
          type="password" 
          show-password
          prefix-icon="el-icon-key"
          class="custom-input"
        />
      </el-form-item>
      
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input 
          v-model="user.confirmPassword" 
          placeholder="请确认新密码" 
          type="password" 
          show-password
          prefix-icon="el-icon-check"
          class="custom-input"
        />
      </el-form-item>
      
      <el-form-item class="form-buttons">
        <el-button 
          type="primary" 
          @click="submit"
          class="submit-btn"
          :loading="loading"
        >
          <i class="el-icon-check"></i> 保存修改
        </el-button>
        <el-button 
          @click="close"
          class="cancel-btn"
        >
          <i class="el-icon-close"></i> 取消
        </el-button>
      </el-form-item>
      
      <div class="form-decoration">
        <div class="decoration-track"></div>
        <div class="decoration-train"></div>
      </div>
    </el-form>
    
    <div class="password-tips">
      <div class="tips-icon"></div>
      <div class="tips-content">
        <p>密码安全提示：</p>
        <ul>
          <li>密码长度应为6-20个字符</li>
          <li>建议使用字母、数字和符号的组合</li>
          <li>定期更换密码可提高账户安全性</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.loading = false;
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>

<style lang="scss" scoped>
.reset-password-container {
  position: relative;
  background: #fff;
  border-radius: 8px;
  padding: 5px 0;
  animation: fadeIn 0.6s ease-out;
  
  .form-header {
    position: relative;
    display: flex;
    align-items: center;
    padding: 0 15px 15px;
    margin-bottom: 20px;
    border-bottom: 1px solid rgba(0,0,0,0.05);
    
    .header-icon {
      width: 24px;
      height: 24px;
      background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
      border-radius: 50%;
      position: relative;
      margin-right: 12px;
      box-shadow: 0 3px 8px rgba(58, 94, 130, 0.3);
      
      &:before {
        content: "";
        position: absolute;
        top: 7px;
        left: 7px;
        width: 10px;
        height: 10px;
        border: 2px solid white;
        border-radius: 50%;
        box-sizing: border-box;
      }
      
      &:after {
        content: "";
        position: absolute;
        bottom: 1px;
        left: 50%;
        transform: translateX(-50%);
        width: 2px;
        height: 8px;
        background-color: white;
        border-radius: 1px;
      }
    }
    
    .header-title {
      font-size: 18px;
      font-weight: 600;
      color: #3a5e82;
      flex: 1;
      background: linear-gradient(90deg, #3a5e82, #4682b4);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    
    .train-decoration {
      position: absolute;
      top: -5px;
      right: 15px;
      width: 30px;
      height: 15px;
      background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
      border-radius: 5px 30px 0 5px;
      opacity: 0.8;
      z-index: 10;
      box-shadow: 0 3px 5px rgba(0,0,0,0.1);
      
      &:before {
        content: "";
        position: absolute;
        top: 3px;
        right: 5px;
        width: 4px;
        height: 4px;
        background-color: rgba(255,255,255,0.8);
        border-radius: 50%;
      }
      
      &:after {
        content: "";
        position: absolute;
        top: 3px;
        right: 15px;
        width: 6px;
        height: 2px;
        background-color: rgba(255,255,255,0.6);
        border-radius: 2px;
      }
    }
  }
  
  .password-form {
    position: relative;
    padding: 10px 25px 30px;
    
    ::v-deep .el-form-item__label {
      color: #606266;
      font-weight: 500;
    }
    
    .custom-input {
      ::v-deep .el-input__inner {
        border-radius: 8px;
        border: 1px solid #e4e7ed;
        padding-left: 40px;
        height: 40px;
        transition: all 0.3s;
        
        &:focus {
          border-color: #4682b4;
          box-shadow: 0 0 0 2px rgba(70, 130, 180, 0.2);
        }
      }
      
      ::v-deep .el-input__prefix {
        left: 10px;
        color: #3a5e82;
      }
    }
    
    .form-buttons {
      margin-top: 30px;
      display: flex;
      justify-content: center;
      
      .submit-btn {
        background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
        border: none;
        padding: 10px 25px;
        font-size: 14px;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(58, 94, 130, 0.3);
        margin-right: 15px;
        transition: all 0.3s;
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 6px 15px rgba(58, 94, 130, 0.4);
        }
        
        &:active {
          transform: translateY(1px);
        }
        
        i {
          margin-right: 5px;
          font-weight: bold;
        }
      }
      
      .cancel-btn {
        padding: 10px 25px;
        font-size: 14px;
        border-radius: 8px;
        border: 1px solid #dcdfe6;
        transition: all 0.3s;
        color: #606266;
        
        &:hover {
          border-color: #c0c4cc;
          color: #3a5e82;
        }
        
        i {
          margin-right: 5px;
        }
      }
    }
    
    .form-decoration {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 16px;
      overflow: hidden;
      
      .decoration-track {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 3px;
        background-image: repeating-linear-gradient(90deg, 
          transparent, transparent 10px, 
          #3a5e82 10px, #3a5e82 15px);
        opacity: 0.3;
        animation: trackMove 15s linear infinite;
      }
      
      .decoration-train {
        position: absolute;
        bottom: 5px;
        left: -30px;
        width: 20px;
        height: 8px;
        background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
        border-radius: 3px 8px 8px 3px;
        animation: miniTrainMove 12s linear infinite;
        z-index: 2;
        
        &:before {
          content: "";
          position: absolute;
          bottom: -2px;
          right: 3px;
          width: 12px;
          height: 4px;
          background-color: rgba(58, 94, 130, 0.2);
          border-radius: 2px;
        }
        
        &:after {
          content: "";
          position: absolute;
          top: 2px;
          left: 3px;
          width: 3px;
          height: 3px;
          background-color: rgba(255,255,255,0.7);
          border-radius: 50%;
        }
      }
    }
  }
  
  .password-tips {
    margin-top: 25px;
    padding: 15px;
    background: linear-gradient(135deg, rgba(58, 94, 130, 0.03) 0%, rgba(70, 130, 180, 0.06) 100%);
    border-radius: 8px;
    display: flex;
    align-items: flex-start;
    border-left: 3px solid #3a5e82;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    
    .tips-icon {
      width: 22px;
      height: 22px;
      background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
      border-radius: 50%;
      margin-right: 12px;
      position: relative;
      flex-shrink: 0;
      margin-top: 2px;
      box-shadow: 0 2px 5px rgba(58, 94, 130, 0.3);
      
      &:before {
        content: "i";
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        color: white;
        font-style: italic;
        font-weight: bold;
        font-size: 14px;
        line-height: 22px;
        text-align: center;
      }
    }
    
    .tips-content {
      flex: 1;
      
      p {
        margin: 0 0 8px;
        color: #3a5e82;
        font-weight: 600;
        font-size: 14px;
      }
      
      ul {
        margin: 0;
        padding-left: 20px;
        
        li {
          color: #606266;
          font-size: 13px;
          margin-bottom: 5px;
          position: relative;
          
          &:last-child {
            margin-bottom: 0;
          }
          
          &:before {
            content: "";
            position: absolute;
            top: 8px;
            left: -14px;
            width: 6px;
            height: 2px;
            background-color: #3a5e82;
            border-radius: 1px;
          }
        }
      }
    }
  }
}

@keyframes trackMove {
  0% { transform: translateX(0); }
  100% { transform: translateX(50px); }
}

@keyframes miniTrainMove {
  0% { left: -30px; opacity: 1; }
  80% { opacity: 1; }
  98% { opacity: 0; left: 100%; }
  99% { opacity: 0; left: -30px; }
  100% { left: -30px; opacity: 1; }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .reset-password-container {
    .form-header {
      padding: 0 10px 12px;
      
      .header-title {
        font-size: 16px;
      }
    }
    
    .password-form {
      padding: 5px 15px 30px;
    }
    
    .password-tips {
      margin-top: 20px;
      padding: 12px;
    }
  }
}
</style>
